import java.io.IOException;
import java.net.*;

public class two {
    public static void main(String[] args) throws IOException {

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                // UDP
                // 创建socket
                DatagramSocket datagramSocket = null;
                try {
                    datagramSocket = new DatagramSocket();
                } catch (SocketException e) {
                    throw new RuntimeException(e);
                }

                //准备通信数据
                String message = "Look at me";
                DatagramPacket datagramPacket = null;
                try {
                    datagramPacket = new DatagramPacket(message.getBytes(), message.getBytes().length, InetAddress.getByName("localhost"), 8888);
                } catch (UnknownHostException e) {
                    throw new RuntimeException(e);
                }

                //发送
                try {
                    datagramSocket.send(datagramPacket);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }


                datagramSocket.close();
            }
        });
        thread.start();


        // 监听端口

        DatagramSocket datagramSocket = new DatagramSocket(8888);

        byte[] bytes = new byte[1024];
        DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length);
        datagramSocket.receive(datagramPacket);
        byte data[] = datagramPacket.getData();
        System.out.println(new String(data, 0, datagramPacket.getLength()) + " " + datagramPacket.getPort() + " ");
    }

}
